package cn.edu.gzccc.movie.service.impl;

import cn.edu.gzccc.movie.domain.Actor;
import cn.edu.gzccc.movie.domain.Movie;
import cn.edu.gzccc.movie.domain.Role;
import cn.edu.gzccc.movie.repository.ActorRepository;
import cn.edu.gzccc.movie.repository.MovieRepository;
import cn.edu.gzccc.movie.repository.RoleRepository;
import cn.edu.gzccc.movie.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private ActorRepository actorRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Override
    public void add(Long MovieId, Long roleId, String name, String introducer, String img) {
        Movie movie = movieRepository.findOne(MovieId);
        Actor actor = actorRepository.findOne(roleId);
        movie.addRole(actor, name, introducer, img);
        movieRepository.save(movie);
    }

    @Override
    public void delete(Long roleId) {
        roleRepository.delete(roleId);
    }

    @Override
    public Role findOne(Long id) {
        Role role = roleRepository.findOne(id);
        return role;
    }

    /**
     * 角色修改
     *
     * @param movieId
     * @param roleId
     * @param name
     * @param actorId
     */
    @Override
    public void edit(Long movieId, Long roleId, String name, Long actorId, String introducer, String img) {
        Role role = roleRepository.findOne(roleId);
        Actor actor = actorRepository.findOne(actorId);
        role.setName(name);
        role.setActor(actor);
        role.setIntroducer(introducer);
        role.setImg(img);
        roleRepository.save(role);
    }


}
